package payment.simulator.institution.tx.matchbatch;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import payment.api.tx.matchbatch.Tx1960Request;


public class Tx1960 extends HttpServlet {


	private static final long serialVersionUID = -3898091515601590658L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 1.取得参数
            String institutionID = request.getParameter("InstitutionID");
            String txSN = request.getParameter("TxSN");
            String payer = request.getParameter("Payer");
            String paymentType = request.getParameter("PaymentType");
            String payerAmount = request.getParameter("PayerAmount");
            String txSNBinding = request.getParameter("TxSNBinding");
            String payerBankID = request.getParameter("PayerBankID");
            String payerAccountType = request.getParameter("PayerAccountType");
            String payerAccountName = request.getParameter("PayerAccountName");
            String payerAccountNumber = request.getParameter("PayerAccountNumber");
            String payerBranchName = request.getParameter("PayerBranchName");
            String payerProvince = request.getParameter("PayerProvince");
            String payerCity = request.getParameter("PayerCity");
            String payerIdentificationType = request.getParameter("PayerIdentificationType");
            String payerIdentificationNumber = request.getParameter("PayerIdentificationNumber");
            String payerCardMediaType = request.getParameter("PayerCardMediaType");
            String payerRemark = request.getParameter("PayerRemark");
            String payee = request.getParameter("Payee");
            String payeeBankID = request.getParameter("PayeeBankID");
            String payeeAmount = request.getParameter("PayeeAmount");
            String payeeAccountType = request.getParameter("PayeeAccountType");
            String payeeAccountName = request.getParameter("PayeeAccountName");
            String payeeAccountNumber = request.getParameter("PayeeAccountNumber");
            String payeeBranchName = request.getParameter("PayeeBranchName");
            String payeeProvince = request.getParameter("PayeeProvince");
            String payeeCity = request.getParameter("PayeeCity");
            String payeeRemark = request.getParameter("PayeeRemark");
            String couponAccountName = request.getParameter("CouponAccountName");
            String couponAccountNumber = request.getParameter("CouponAccountNumber");
            String couponAmount = request.getParameter("CouponAmount");
            String profitAmount = request.getParameter("ProfitAmount");
            String refundRemark = request.getParameter("RefundRemark");

            // 2.创建交易请求对象
            Tx1960Request tx1960Request = new Tx1960Request();
            tx1960Request.setInstitutionID(institutionID);
            tx1960Request.setTxSN(txSN);
            tx1960Request.setPayer(payer);
            tx1960Request.setPaymentType(paymentType);
            tx1960Request.setPayerAmount(payerAmount);
            tx1960Request.setTxSNBinding(txSNBinding);
            tx1960Request.setPayerBankID(payerBankID);
            tx1960Request.setPayerAccountType(payerAccountType);
            tx1960Request.setPayerAccountName(payerAccountName);
            tx1960Request.setPayerAccountNumber(payerAccountNumber);
            tx1960Request.setPayerBranchName(payerBranchName);
            tx1960Request.setPayerProvince(payerProvince);
            tx1960Request.setPayerCity(payerCity);
            tx1960Request.setPayerIdentificationType(payerIdentificationType);
            tx1960Request.setPayerIdentificationNumber(payerIdentificationNumber);
            tx1960Request.setPayerCardMediaType(payerCardMediaType);
            tx1960Request.setPayerRemark(payerRemark);
            tx1960Request.setPayee(payee);
            tx1960Request.setPayeeBankID(payeeBankID);
            tx1960Request.setPayeeAmount(payeeAmount);
            tx1960Request.setPayeeAccountType(payeeAccountType);
            tx1960Request.setPayeeAccountName(payeeAccountName);
            tx1960Request.setPayeeAccountNumber(payeeAccountNumber);
            tx1960Request.setPayeeBranchName(payeeBranchName);
            tx1960Request.setPayeeProvince(payeeProvince);
            tx1960Request.setPayeeCity(payeeCity);
            tx1960Request.setPayeeRemark(payeeRemark);
            tx1960Request.setCouponAccountName(couponAccountName);
            tx1960Request.setCouponAccountNumber(couponAccountNumber);
            tx1960Request.setCouponAmount(couponAmount);
            tx1960Request.setProfitAmount(profitAmount);
            tx1960Request.setRefundRemark(refundRemark);


            // 3.执行报文处理
            tx1960Request.process();

            // 4.将参数放置到request对象
            request.setAttribute("plainText", tx1960Request.getRequestPlainText());
            request.setAttribute("message", tx1960Request.getRequestMessage());
            request.setAttribute("signature", tx1960Request.getRequestSignature());
            request.setAttribute("txCode", "1960");
            request.setAttribute("txName", "一站式实时收付款");
            request.setAttribute("action", request.getContextPath() + "/SendMessage");

            // 5.转向Request.jsp页面
            request.getRequestDispatcher("/Request.jsp").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
            processException(request, response, e.getMessage());
        }
    }

    private void processException(HttpServletRequest request, HttpServletResponse response, String errorMessage) {
        try {
            request.setAttribute("errorMessage", errorMessage);
            request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response);
        } catch (Exception e) {
        }
    }

}
